package com.util;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.config.Constant;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiProcessinstanceCreateRequest;
import com.dingtalk.api.request.OapiProcessinstanceGetRequest;
import com.dingtalk.api.request.OapiProcessinstanceListidsRequest;
import com.dingtalk.api.response.OapiProcessinstanceCreateResponse;
import com.dingtalk.api.response.OapiProcessinstanceGetResponse;
import com.dingtalk.api.response.OapiProcessinstanceGetResponse.ProcessInstanceTopVo;
import com.dingtalk.api.response.OapiProcessinstanceListidsResponse;
import com.taobao.api.ApiException;

/**
 * 审批工具类
 * 
 *@author zhonglj
 */
public class processUtils {
    private static final Logger bizLogger = LoggerFactory.getLogger(processUtils.class);
    
    public static void applyProcess() {
        DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/processinstance/create");
        OapiProcessinstanceCreateRequest request = new OapiProcessinstanceCreateRequest();
        request.setAgentId(Constant.AGENTID);
        request.setProcessCode("PROC-E03755C0-96B4-40EB-A817-0B31E3A75F2F");
        // 审批流表单参数，设置各表单项值
        List<OapiProcessinstanceCreateRequest.FormComponentValueVo> formComponentValues = new ArrayList<OapiProcessinstanceCreateRequest.FormComponentValueVo>();

        // 加班人
        OapiProcessinstanceCreateRequest.FormComponentValueVo vo1 = new OapiProcessinstanceCreateRequest.FormComponentValueVo();
        vo1.setName("加班人");
        vo1.setValue("[\"manager4547\"]");

        // 加班区间
        OapiProcessinstanceCreateRequest.FormComponentValueVo vo2 = new OapiProcessinstanceCreateRequest.FormComponentValueVo();
        vo2.setName("[\"开始时间\",\"结束时间\"]");
        vo2.setValue("[\"2019-11-02 18:00\",\"2019-11-02 19:00\"]");

        // 加班原因
        OapiProcessinstanceCreateRequest.FormComponentValueVo vo3 = new OapiProcessinstanceCreateRequest.FormComponentValueVo();
        vo3.setName("加班原因");
        vo3.setValue("接口发起审批测试666");
        
        // 附件

        // 添加单行输入框、多行输入框、图片、明细、附件到表单
        formComponentValues.add(vo1);
        formComponentValues.add(vo2);
        formComponentValues.add(vo3);

        request.setFormComponentValues(formComponentValues);
        // 使用后台模版的情况下不用填写
//        request.setApprovers("userid1,userid2");
        request.setOriginatorUserId("manager4547");
        request.setDeptId(-1L);
//        request.setCcList("userid1,userid2");
        request.setCcPosition("START_FINISH");
        try {
            OapiProcessinstanceCreateResponse response = client.execute(request,AuthHelper.getAccessToken());
            if (response.getErrcode().intValue() == 0) {
                bizLogger.info("apply process success!!!!");
            }else {
                bizLogger.error(response.getErrmsg());
            }
        } catch (ApiException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * "PROC-E03755C0-96B4-40EB-A817-0B31E3A75F2F" 加班
     * PROC-FB68E9CE-CAB9-4693-A922-1A4BF887F537 请假
     * PROC-A58FB763-8AB6-4A50-B3D1-7265EF6C4ABD test
     * 
     * @param processCode
     * @return List<String>
     * @author zhonglj
     */
    public static List<String> getProcess(String processCode) {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/processinstance/listids");
        OapiProcessinstanceListidsRequest req = new OapiProcessinstanceListidsRequest();
        req.setProcessCode(processCode);
        req.setStartTime(1575158400000L);
        req.setEndTime(1577750400000L);
        req.setSize(10L);
        req.setCursor(0L);
        req.setUseridList("manager4547");
        try {
            OapiProcessinstanceListidsResponse response = client.execute(req, AuthHelper.getAccessToken());
            if (response.getErrcode().intValue() == 0) {
                List<String> list = response.getResult().getList();
                bizLogger.info(list.toString());
                return list;
            }
        } catch (ApiException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
    
    public static void getProcessDetail(String id) {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/processinstance/get");
        OapiProcessinstanceGetRequest request = new OapiProcessinstanceGetRequest();
        request.setProcessInstanceId(id);
        try {
            OapiProcessinstanceGetResponse response = client.execute(request,AuthHelper.getAccessToken());
            if (response.getErrcode().intValue() == 0) {
                ProcessInstanceTopVo processInstance = response.getProcessInstance();
                bizLogger.info(JSON.toJSONString(processInstance));
            }else {
                bizLogger.warn(response.getErrmsg());
            }
        } catch (ApiException e) {
            e.printStackTrace();
        }
    }
}
